package main

import (
	"distribute-mapreduce/common/mrtype"
	"strconv"
	"strings"
	"unicode"
)

func Map(filename string, contents string) []mrtype.KeyValue {
	separatorFunc := func(r rune) bool {
		return !unicode.IsLetter(r)
	}

	words := strings.FieldsFunc(contents, separatorFunc)

	kva := make([]mrtype.KeyValue, 0)
	for _, w := range words {
		kv := mrtype.KeyValue{Key: w, Value: "1"}
		kva = append(kva, kv)
	}

	return kva
}

func Reduce(key string, values []string) string {
	return strconv.Itoa(len(values))
}
